package com.yun.algorithmproblem.leetcode;

public class Leetcode3095 {

    public static void main(String[] args) {
        Leetcode3095 obj=new Leetcode3095();
        int[] nums=new int[]{1,2,3};
        int k=2;
        System.out.println(obj.minimumSubarrayLength(nums, k));
    }
    public int minimumSubarrayLength(int[] nums, int k) {
        int[] bits=new int[7];
        int ans=Integer.MAX_VALUE;
        int left=0,right=0;
        for (;right<nums.length;right++){
            for (int i = 0; i < 7; i++) {
                bits[i]+=(nums[right]>>i)&1;
            }
            while (left<=right&&calc(bits)>=k){
                ans=Math.min(ans,right-left+1);
                for (int i = 0; i < 7; i++) {
                    bits[i]-=(nums[left]>>i)&1;
                }
                left++;
            }
        }
        return ans==Integer.MAX_VALUE?-1:ans;
    }

    private int calc(int[] bits){
        int ans=0;
        for (int i = 0; i < bits.length; i++) {
            if(bits[i]>0){
                ans|=1<<i;
            }
        }
        return ans;
    }
}
